package com.lik888.web1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lik888.web1.constants.UserContext;
import com.lik888.web1.entity.GoodsTypeimgtable;
import com.lik888.web1.entity.UserTable;
import com.lik888.web1.mapper.GoodsTypeimgtableMapper;
import com.lik888.web1.mapper.UserTableMapper;
import com.lik888.web1.service.IGoodsTypeimgtableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-11-06
 */
@Service
@RequiredArgsConstructor
public class GoodsTypeimgtableServiceImpl extends ServiceImpl<GoodsTypeimgtableMapper, GoodsTypeimgtable> implements IGoodsTypeimgtableService {
    private final GoodsTypeimgtableMapper goodsTypeimgtableMapper;
    private final UserTableMapper userTableMapper;
    @Override
    @Transactional
    public List<String> listImg(List<String> goodsTypes) throws RuntimeException {
        //获取当前用户
        Long userId = UserContext.getUser();
        //查询用户对象
        UserTable userTable = userTableMapper.selectById(userId);
        //获取用户名，方便调试
        String username = userTable != null ? userTable.getUsername() : null;

        List<String> imgList = new ArrayList<>();
       for (String goodsType : goodsTypes) {
           GoodsTypeimgtable goodsTypeimgtable = goodsTypeimgtableMapper.selectOne(new LambdaQueryWrapper<GoodsTypeimgtable>()
                   .eq(GoodsTypeimgtable::getGoodsType, goodsType));
           if (goodsTypeimgtable != null) {
               imgList.add(goodsTypeimgtable.getGoodsTypeimg());
           } else {
               //等于空时
               //创建新的对象
               GoodsTypeimgtable goodsTypeimgtable1 = new GoodsTypeimgtable();
               goodsTypeimgtable1.setGoodsType(goodsType);
               goodsTypeimgtable1.setGoodsTypeimg("https://java-ai-web-lik.oss-cn-chengdu.aliyuncs.com/0a74ca46-63d1-4ce1-8eb3-763cdc95dc4b.jpg");
               goodsTypeimgtableMapper.insert(goodsTypeimgtable1);
               imgList.add(goodsTypeimgtable1.getGoodsTypeimg());
           }
       }
       return imgList;
    }
}
